define(['app', 'ace-language-tools'], function(app) {
    app.controller('MockGitDiffController', function($scope, $compile, $http, xContext, $uibModalInstance) {
        var self = this;
        self.file = '';
        self.fullScreen = false;

        var dataViewer = null;

        self.init = function() {
            self.file = xContext.file;
            // Data viewer
            dataViewer = ace.edit('data-viewer', {
                mode: 'ace/mode/diff',
                theme: 'ace/theme/chrome',
                fontSize: 14,
                readOnly: true,
                useWorker: false,
                showPrintMargin: false,
                showFoldWidgets: false,
                displayIndentGuides: false,
                highlightGutterLine: false,
                highlightActiveLine: false,
                autoScrollEditorIntoView: true
            });

            $('#data-viewer .ace_scroller').append(
                    $compile($('#viewer-tools').html())($scope)
                );
            $('#data-viewer .x-editor-tool').mousedown(function(e) { return false; });

            $scope.$watch('dvm.fullScreen', function(fullScreen) {
                if (fullScreen) {
                    $('.modal-dialog:eq(0)').toggleClass('x-modal-max');
                } else {
                    $('.modal-dialog:eq(0)').removeClass('x-modal-max');
                }
                setTimeout(function() {
                    dataViewer.resize();
                }, 50);
            });

            $http.post('/mock/git/diff', {
                file: xContext.path,
                sha: xContext.sha,
                shb: xContext.shb
            }).then(function(response) {
                if (response.status == 200) {
                    var diffLines = response.data;
                    if (!_.isEmpty(diffLines)) {
                        var editorSession = dataViewer.getSession();
                        // Line number
                        editorSession.gutterRenderer = {
                            getDiffText: function(session, row) {
                                var line = diffLines[row];
                                if (line) {
                                    return [line.oldLineno, line.newLineno];
                                }
                                return [row, row];
                            },
                            getWidth: function(session, lastLineNumber, config) {
                                return Math.ceil(lastLineNumber.toString().length * config.characterWidth + 10) * 2;
                            }
                        };
                        // Highlight
                        _.forEach(diffLines, function(line, row) {
                            var className = (line.origin == '+' ? 'x-git-add' :
                                    (line.origin == '-' ? 'x-git-remove' : 'x-git-line'));
                            editorSession.addGutterDecoration(row, className);
                            editorSession.highlightLines(row, row, className, false);
                        });
                        // Set value
                        dataViewer.setValue(_.map(diffLines, function(line) {
                            return (line.origin == '@' ? '' : line.origin) + line.content;
                        }).join('\n'), -1);
                        dataViewer.resize();
                    }
                }
            });
        }

        self.close = function() {
            $uibModalInstance.close();
        }
    });
});
